﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmListView02 : Form
    {
        public FrmListView02()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 保持分组信息-用于分组使用-没用上
        /// </summary>
        Dictionary<string, string> dicGroups = new Dictionary<string, string>();
        private void FrmListView02_Load(object sender, EventArgs e)
        {
            LoadImgList();//加载图片资源

            //分组的初始化
            lvList.Groups.Clear();
            //添加组
            //lvList.Groups.Add(new ListViewGroup("动物", HorizontalAlignment.Left));
            //lvList.Groups.Add(new ListViewGroup("人物", HorizontalAlignment.Center));
            //lvList.Groups.Add(new ListViewGroup("风景", HorizontalAlignment.Left));
            //lvList.Groups.Add(new ListViewGroup("花", HorizontalAlignment.Right));
            //编号  组名  联系  ---关系
            //dicGroups.Add("动物", "01");
            //dicGroups.Add("人物", "02");
            //dicGroups.Add("风景", "03");
            //dicGroups.Add("花", "04");
            //添加组-添加分组信息
            lvList.Groups.Add("01", "动物");
            lvList.Groups.Add("02", "人物");
            lvList.Groups.Add("03", "风景");
            lvList.Groups.Add("04", "花");
            foreach(ListViewGroup g in lvList.Groups)
            {
                g.HeaderAlignment = HorizontalAlignment.Center; // 设置分组文本对齐方式
            }

            lvList.Items.Clear(); // 清空列表
            lvList.ShowGroups = false;//不以分组显示
            if(largeList !=null && largeList.Images.Count>0) // 判断图片资源
            {
                for (int i = 0; i < largeList.Images.Count; i++) // 列表循环添加图片项
                {
                    //添加ListViewItem
                    string iText = largeList.Images.Keys[i];//键名-获取图片名
                    ListViewItem li = new ListViewItem(); // 列表项
                    li.Text = iText; // 显示文本
                    li.ImageIndex = i; // 图像索引- 下边添加了图像资源-所以通过索引可以关联到图像资源
                    lvList.Items.Add(li); // 添加项
                }
            }
            lvList.View = View.LargeIcon;// 设置大图标模式
            lvList.LargeImageList = largeList; // 绑定大图资源
            lvList.SmallImageList = smallList; // 绑定小图资源
        }

        /// <summary>
        /// 存放key和图片路径-详情列表会使用到
        /// </summary>
        Dictionary<int, string> dic = new Dictionary<int, string>();
        /// <summary>
        /// 图片资源加载
        /// </summary>
        private void LoadImgList()
        {
            string path = @"Imgs";
            if(Directory.Exists(path))//判断文件夹是否已存在
            {
                string[] files = Directory.GetFiles(path); // 根据资源获取图片路径
                if(files.Length>0)
                {
                    //图片集合中添加图像资源
                    largeList.Images.Clear();
                    smallList.Images.Clear();
                    string[] fileType = { ".jpg", ".png", ".bmp", ".ico" };
                    int index = 0;
                    foreach (string fPath in files)  // 循环添加图片
                    {
                        if(fileType.Contains(Path.GetExtension(fPath))) // 判断图片类型是否存在
                        {
                            Image img = Image.FromFile(fPath);  // 加载图片资源
                            //图片资源的键名
                            string key = Path.GetFileNameWithoutExtension(fPath);  // 
                            //将图片资源添加到大图标、小图标图片集合中
                            largeList.Images.Add(key, img); // 添加大图标
                            smallList.Images.Add(key, img); // 添加小图标
                            dic.Add(index, fPath); // 添加字典-详情列表会用到
                            index++;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 大图标视图模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLarge_Click(object sender, EventArgs e)
        {
            lvList.View = View.LargeIcon;//大图标
        }

        /// <summary>
        /// 小图表视图模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSmall_Click(object sender, EventArgs e)
        {
            lvList.View = View.SmallIcon;//小图标
        }

        /// <summary>
        /// 列表视图模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnList_Click(object sender, EventArgs e)
        {
            lvList.View = View.List;//小图标
        }

        /// <summary>
        /// 平铺模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTile_Click(object sender, EventArgs e)
        {
            lvList.View = View.Tile;//小图标
        }

        /// <summary>
        /// 详情视图模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDetails_Click(object sender, EventArgs e)
        {
            lvList.View = View.Details;//详细信息
            lvList.Columns.Clear(); // 清空列标题集合
            lvList.Items.Clear(); // 清空项集合
            lvList.Columns.Add("文件名", 100, HorizontalAlignment.Left);//添加列标题
            lvList.Columns.Add("创建日期", 150, HorizontalAlignment.Left);//添加列标题
            lvList.Columns.Add("类型", 100, HorizontalAlignment.Left);//添加列标题
            lvList.Columns.Add("大小", 100, HorizontalAlignment.Left);//添加列标题
            for (int i = 0; i < dic.Count; i++) // 循环添加列内容
            {
                ListViewItem li = new ListViewItem(); // 项
                li.ImageIndex = i; // 图片索引
                li.Text = smallList.Images.Keys[i]; // 显示图片名称
                li.SubItems.Add(File.GetCreationTime(dic[i]).ToString());//获取创建日期
                li.SubItems.Add(Path.GetExtension(dic[i]));//类型名
                long length = new FileInfo(dic[i]).Length;//获取文件大小
                li.SubItems.Add((length / 1024).ToString());//文件大小   KB
                if(lvList.ShowGroups) // 判断是否分组
                {
                    string key =li.Text.Substring(0, 2);
                    ListViewGroup group = lvList.Groups[key];
                    if (group != null)
                    {
                        lvList.Items[i].Group = group;
                    }
                }
                lvList.Items.Add(li);
            }
            lvList.GridLines = true; // 设置显示网格线
        }

        /// <summary>
        /// 启用分组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGroup_Click(object sender, EventArgs e)
        {
            lvList.ShowGroups = true;//启用分组
            //将项处理它所属的分组
            ShowGroups();
        }

        /// <summary>
        /// 将项处理它所属的分组
        /// </summary>
        private void ShowGroups()
        {
            for (int i = 0; i < lvList.Items.Count; i++) // 循环列表项
            {
                string key=  lvList.Items[i].Text.Substring(0, 2); // 这里是把图片名称进行分组命名了
                ListViewGroup group = lvList.Groups[key];  // 根据key获取分组
                if(group!=null)
                {
                    lvList.Items[i].Group = group; // 设置项的分组
                }
            }
        }
    }
}
